package net.yxsoft.controller.common.validator;

import com.jfinal.kit.StrKit;
import com.jfinal.validate.Validator;

/**
 * Created by YXLF on 2016/6/29.
 */
public abstract class BaseValidator extends Validator {


  /**
   * Validate string.
   */
  protected void validateByteString(String field, int minLen, int maxLen, String errorKey, String errorMessage) {
    validateStringValue(controller.getPara(field), minLen, maxLen, errorKey, errorMessage);
  }

  private void validateStringValue(String value, int minLen, int maxLen, String errorKey, String errorMessage) {
    if (StrKit.isBlank(value)) {
      value = "";
    }
    if (value.getBytes().length < minLen || value.getBytes().length > maxLen)
      addError(errorKey, errorMessage);
  }
}
